﻿using NCP.Infrastructure.Common;
using NCP.Infrastructure.Common.Domain;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.Bookmarks;
using NCP.Recruiting.ApplicationServices.ViewModels;
using NCP.Recruiting.Domain.Aggregates.Bookmarks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NCP.Recruiting.ApplicationServices.Implementations
{
    public class BookmarkAppService : IBookmarkAppService
    {
        private readonly IBookmarkRepository _bookmarkRepository;
        private const String _dateFormat = "yyyy/MM/dd";
        private const String _dateFormatTime = "yyyy/MM/dd HH:mm:ss";

        public BookmarkAppService(IBookmarkRepository bookmarkRepository)
        {
            _bookmarkRepository = bookmarkRepository;
        }

        public CreateBookmarkResponse Create(CreateBookmarkRequest request)
        {
            var response = new CreateBookmarkResponse();
            try
            {
                var model = new Bookmark()
                {
                    CompanyId = request.CompanyId,
                    ResumeId = request.ResumeId,
                    MarkedOn = DateTime.Now
                };
                model.GenerateNewIdentity();
                _bookmarkRepository.Add(model);
                _bookmarkRepository.UnitOfWork.Commit();
            }
            catch (Exception ex)
            {
                response.Exception = ex;
            }
            return response;
        }

        public DeleteBookmarkResponse Delete(DeleteBookmarkRequest request)
        {
            var response = new DeleteBookmarkResponse();
            try
            {
                var model = _bookmarkRepository.Get(request.Id);
                _bookmarkRepository.Remove(model);
                _bookmarkRepository.UnitOfWork.Commit();
            }
            catch (Exception ex)
            {
                response.Exception = ex;
            }
            return response;
        }


        public GetBookmarksResponse GetBookmarks(GetBookmarksRequest request)
        {
            var response = new GetBookmarksResponse();
            try
            {
                var specifications = BookmarkSpecifications.GetByCompany(request.CompanyId);
                var list = _bookmarkRepository.GetPagedList(request.PageIndex, request.PageSize, specifications, x => x.MarkedOn, false);
                var emptyLabel = "未知";
                var listItemModelList = list.Select(x => new BookmarkListItemModel()
                {
                    Age = !x.Resume.BirthDate.HasValue ? emptyLabel : (DateTime.Now.Year - x.Resume.BirthDate.Value.Year - ((DateTime.Now.Month * 100 + DateTime.Now.Day) < (x.Resume.BirthDate.Value.Month * 100 + x.Resume.BirthDate.Value.Day) ? 1 : 0)).ToString(),
                    CurrentJob = x.Resume.CurrentJob,
                    DesiredIndustry = !x.Resume.DesiredIndustry.HasValue ? emptyLabel : LovHandler.GetNameByLovValue(x.Resume.DesiredIndustry.Value),
                    EducationLevel = !x.Resume.EducationLevel.HasValue ? emptyLabel : LovHandler.GetNameByLovValue(x.Resume.EducationLevel.Value),
                    Gender = x.Resume.Gender,
                    Id = x.Id,
                    ResumeId = x.ResumeId,
                    LastUpdate = x.Resume.LastUpdated.ToString(_dateFormat),
                    Major = !x.Resume.Major.HasValue ? emptyLabel : LovHandler.GetNameByLovValue(x.Resume.Major.Value),
                    Name = x.Resume.FullName,
                    Telephone = x.Resume.Tel,
                    YearsOfExperience = !x.Resume.YearsOfExperience.HasValue ? emptyLabel : LovHandler.GetNameByLovValue(x.Resume.YearsOfExperience.Value),
                });
                response.ListItemModelList = new PagedList<BookmarkListItemModel>(listItemModelList, list.PageIndex, list.PageSize, list.TotalCount);
            }
            catch (Exception ex)
            {
                response.Exception = ex;
            }
            return response;
        }
    }
}
